using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using LeisureCenter.Core;
using LeisureCenter.Api;
using LeisureCenter.BrowserItems;
using LeisureCenter.Controls;

namespace LeisureCenter.Forms
{
    public partial class MainForm : Form
    {
        private SlidePosition slidePosition;
        private IMenuItem lastMainMenuItem;
        private IMenuItem lastSubmenuItem;
        private List<IBrowserItem> browserItems = new List<IBrowserItem>();
        private IBrowserFolderItem browserItemsFolder;
        private IBrowserFolderItem browserItemsParent;
        private ScreensaverForm screensaverForm = new ScreensaverForm();
        private Point lastCursorPos = Cursor.Position;
        private bool screensaveEnabled = true;

        public MainForm()
        {
            InitializeComponent();
            Program.LeisureCenterInstance.PlaylistManager.PlayModeChanged += new EventHandler(PlaylistManager_PlayModeChanged);
            Program.LeisureCenterInstance.PlaylistManager.PlayStateChanged += new EventHandler<PlayStateChangeEventArgs>(PlaylistManager_PlayStateChanged);
            Program.LeisureCenterInstance.PlaylistManager.PlaylistChanged += new EventHandler(PlaylistManager_PlaylistChanged);
            Program.LeisureCenterInstance.PlaylistManager.ActivePlaylistItemChanged += new EventHandler(PlaylistManager_ActivePlaylistItemChanged);
            Program.LeisureCenterInstance.PlaylistManager.PositionChanged += new EventHandler(PlaylistManager_PositionChanged);
#if DEBUG
            WindowState = FormWindowState.Normal;
            FormBorderStyle = FormBorderStyle.Sizable;

            miCrash.Visible = true;
            sepCrash.Visible = true;
            miTest.Visible = true;
#endif
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            Padding = Program.LeisureCenterInstance.Settings.Padding;
            pnlMainMenu.Width = 260;
            pnlSubmenu.Width = pnlMainMenu.Width;
            pnlBrowserAndInfo.Width = pnlMain.Width - SLIDE_MARGIN * 2;
            pnlPlaylistAndPreview.Width = pnlBrowserAndInfo.Width;
            pnlCustomScreens.Width = pnlBrowserAndInfo.Width;
            pnlSlider.Width = pnlMainMenu.Width + pnlSubmenu.Width + pnlBrowserAndInfo.Width + pnlPlaylistAndPreview.Width;
            lbMainMenu.Images = Program.LeisureCenterInstance.IconsManager[IconSize.s96x96];
            lbSubmenu.Images = Program.LeisureCenterInstance.IconsManager[IconSize.s96x96];
            lbBrowser.Images = Program.LeisureCenterInstance.IconsManager[IconSize.s32x32];
            lbPlaylist.Images = Program.LeisureCenterInstance.IconsManager[IconSize.s32x32];
            Program.LeisureCenterInstance.MenuManager.RefreshMainMenu();
            timerDateTime_Tick(timerDateTime, null);
        }

        private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            Program.LeisureCenterInstance.PlaylistManager.PlayModeChanged -= new EventHandler(PlaylistManager_PlayModeChanged);
            Program.LeisureCenterInstance.PlaylistManager.PlayStateChanged -= new EventHandler<PlayStateChangeEventArgs>(PlaylistManager_PlayStateChanged);
            Program.LeisureCenterInstance.PlaylistManager.PlaylistChanged -= new EventHandler(PlaylistManager_PlaylistChanged);
            Program.LeisureCenterInstance.PlaylistManager.ActivePlaylistItemChanged -= new EventHandler(PlaylistManager_ActivePlaylistItemChanged);
            Program.LeisureCenterInstance.PlaylistManager.PositionChanged -= new EventHandler(PlaylistManager_PositionChanged);
        }

        #region screensaver

        public void StartIdle()
        {
            screensaverForm.Hide();
            timerScreensaver.Stop();
            timerScreensaver.Start();
        }

        public void ShowScreensaver()
        {
            if (this.screensaveEnabled && ActiveForm == this)
                screensaverForm.Show();
        }

        private void timerScreensaver_Tick(object sender, EventArgs e)
        {
            timerScreensaver.Stop();
            ShowScreensaver();
        }

        public void DisableScreensaver()
        {
            this.screensaveEnabled = false;
        }

        public void EnableScreensaver()
        {
            this.screensaveEnabled = true;
        }

        #endregion

        #region playlist support

        void PlaylistManager_PositionChanged(object sender, EventArgs e)
        {
            RefreshPosition();
        }

        private void RefreshPosition()
        {
            IPlaylistItem playlistItem = Program.LeisureCenterInstance.PlaylistManager.Playlist.ActivePlaylistItem;
            if (playlistItem == null)
            {
                pbPosition.Position = 0;
                lblPosition.Text = "";
            }
            else
            {
                pbPosition.Position = playlistItem.Position;
                pbPosition.Max = playlistItem.Duration;

                string position;
                string duration;

                DateTime positionDateTime = new DateTime();
                positionDateTime = positionDateTime.AddSeconds(playlistItem.Position);
                DateTime durationDateTime = new DateTime();
                durationDateTime = durationDateTime.AddSeconds(playlistItem.Duration);

                if (playlistItem.Duration >= 3600)
                {
                    position = positionDateTime.ToString("H:mm:ss");
                    duration = durationDateTime.ToString("H:mm:ss");
                }
                else
                {
                    position = positionDateTime.ToString("mm:ss");
                    duration = durationDateTime.ToString("mm:ss");
                }

                lblPosition.Text = String.Format(@"{0}/{1} {2}/{3}", playlistItem.Index + 1, Program.LeisureCenterInstance.PlaylistManager.Playlist.Count, position, duration);
            }
            Program.LeisureCenterInstance.OsdForm.pbPosition.Position = pbPosition.Position;
            Program.LeisureCenterInstance.OsdForm.pbPosition.Max = pbPosition.Max;
            Program.LeisureCenterInstance.OsdForm.lblPosition.Text = lblPosition.Text;
        }

        void PlaylistManager_ActivePlaylistItemChanged(object sender, EventArgs e)
        {
            lbPlaylist.PlayingIndex = Program.LeisureCenterInstance.PlaylistManager.Playlist.ActivePlaylistItemIndex;
            RefreshPosition();
        }

        void PlaylistManager_PlaylistChanged(object sender, EventArgs e)
        {
            RefreshPlaylistListBox();
        }

        private void RefreshPlaylistListBox()
        {
            lbPlaylist.BeginUpdate();
            try
            {
                lbPlaylist.Items.Clear();
                foreach (IPlaylistItem playlistItem in Program.LeisureCenterInstance.PlaylistManager.Playlist)
                {
                    lbPlaylist.Items.Add(new PlaylistListBoxItem(lbPlaylist, playlistItem));
                }
                lbPlaylist.PlayingIndex = Program.LeisureCenterInstance.PlaylistManager.Playlist.ActivePlaylistItemIndex;
            }
            finally
            {
                lbPlaylist.EndUpdate();
            }
            pnlPlaylistAndPreview.Visible = lbPlaylist.Items.Count > 0;
            if (!pnlPlaylistAndPreview.Visible && slidePosition == SlidePosition.Playlist)
                SlidePosition = SlidePosition.Browser;
        }

        void PlaylistManager_PlayStateChanged(object sender, PlayStateChangeEventArgs e)
        {
            RefreshPlayState(e.NewPlayState);
        }

        private void RefreshPlayState(MediaPlayState playState)
        {
            switch (playState)
	        {
		        case MediaPlayState.Stopped:
                    pbPosition.Position = 0;
                    lblPlayState.Text = "<";
                    break;
                case MediaPlayState.Paused:
                    lblPlayState.Text = ";";
                    break;
                case MediaPlayState.Playing:
                    lblPlayState.Text = "4";
                    pbPosition.Min = 0;
                    if (Program.LeisureCenterInstance.PlaylistManager.Playlist.ActivePlaylistItem != null)
                        pbPosition.Max = Program.LeisureCenterInstance.PlaylistManager.Playlist.ActivePlaylistItem.Duration;
                    break;
                case MediaPlayState.FastForward:
                    lblPlayState.Text = "8";
                    break;
                case MediaPlayState.FastReverse:
                    lblPlayState.Text = "7";
                    break;
                default:
                    break;
	        }
        }

        void PlaylistManager_PlayModeChanged(object sender, EventArgs e)
        {
            tbRepeat.Down = (Program.LeisureCenterInstance.PlaylistManager.PlayMode & PlayMode.Repeat) > 0;
            tbRandom.Down = (Program.LeisureCenterInstance.PlaylistManager.PlayMode & PlayMode.Random) > 0;
        }

        private void lbPlaylist_ItemSelected(object sender, EventArgs e)
        {
            BeginWait();
            try
            {
                Program.LeisureCenterInstance.PlaylistManager.Playlist.ActivePlaylistItemIndex = lbPlaylist.SelectedIndex;
            }
            finally
            {
                EndWait();
            }
        }

        #endregion

        #region menu and browser fillers and events

        public void BrowseTo(IBrowserFolderItem folder, bool focus, IBrowserItem selectItem)
        {
            if (folder == browserItemsParent)
                selectItem = browserItemsFolder;
            lbBrowser.BeginUpdate();
            try
            {
                browserItems.Clear();
                lbBrowser.Items.Clear();
                browserItemsParent = null;

                if (this.browserItemsFolder != null)
                    browserItemsFolder.ItemsChanged -= new EventHandler(browserItemsFolder_ItemsChanged);
                browserItemsFolder = folder;
                if (this.browserItemsFolder != null)
                    browserItemsFolder.ItemsChanged += new EventHandler(browserItemsFolder_ItemsChanged);
                lblBrowserTitle.Text = "";
                if (folder != null)
                {
                    lblBrowserTitle.Text = folder.Title;
                    if (folder.Folder != null)
                    {
                        lbBrowser.Items.Add(new ListBoxItem(lbBrowser, "...", "folder"));
                        browserItems.Add(folder.Folder);
                        browserItemsParent = folder.Folder;
                    }
                    IEnumerable<IBrowserItem> items = folder.Items;
                    browserItems.AddRange(items);
                    foreach (IBrowserItem item in items)
                    {
                        lbBrowser.Items.Add(new ListBoxItem(lbBrowser, item.Text, item.IconName));
                        if (item == selectItem)
                            lbBrowser.SelectedIndex = lbBrowser.Items.Count - 1;
                        if (item.Folder != null && item.Folder != folder)
                        {
                            folder = item.Folder;
                            lblBrowserTitle.Text = folder.Title;
                        }
                    }
                }
            }
            finally
            {
                lbBrowser.EndUpdate();
            }
            pnlBrowserAndInfo.Visible = lbBrowser.Items.Count > 0;
            if (pnlBrowserAndInfo.Visible)
                pnlCustomScreens.Hide();
            if (lbBrowser.Items.Count > 1 && browserItemsParent != null && lbBrowser.SelectedIndex == 0)
                    lbBrowser.SelectedIndex = 1;
            if (focus && lbBrowser.CanFocus)
                lbBrowser.Focus();
        }

        void browserItemsFolder_ItemsChanged(object sender, EventArgs e)
        {
            IBrowserItem selectedItem = null;
            if (lbBrowser.SelectedIndex > -1)
                selectedItem = this.browserItems[lbBrowser.SelectedIndex];
            BrowseTo(this.browserItemsFolder, false, selectedItem);
        }

        public void BrowseToParent()
        {
            if (browserItemsParent != null)
                BrowseTo(browserItemsParent, true, browserItemsFolder);
        }

        internal void RefreshFolder()
        {
            if (browserItemsFolder != null)
            {
                browserItemsFolder.Refresh();
            }
        }

        public void FillSubmenu(IEnumerable<IMenuItem> items)
        {
            lbSubmenu.BeginUpdate();
            try
            {
                lbSubmenu.Items.Clear();
                if (items != null)
                {
                    foreach (IMenuItem item in items)
                    {
                        lbSubmenu.Items.Add(new MenuItemListBoxItem(lbSubmenu, item));
                    }
                }
            }
            finally
            {
                lbSubmenu.EndUpdate();
            }
            pnlSubmenu.Visible = lbSubmenu.Items.Count > 0;
            if (lbSubmenu.CanFocus)
                lbSubmenu.Focus();
        }

        public void FillMainMenu(IEnumerable<IMenuItem> items)
        {
            IMenuItem lastSelectedItem = null;
            if (lbMainMenu.SelectedItem != null)
                lastSelectedItem = ((MenuItemListBoxItem)lbMainMenu.SelectedItem).Item;

            lbMainMenu.BeginUpdate();
            try
            {
                lbMainMenu.Items.Clear();
                foreach (IMenuItem item in items)
                {
                    lbMainMenu.Items.Add(new MenuItemListBoxItem(lbMainMenu, item));
                    if (item == lastSelectedItem)
                        lbMainMenu.SelectedIndex = lbMainMenu.Items.Count - 1;
                }
            }
            finally
            {
                lbMainMenu.EndUpdate();
            }
        }

        private void lbMainMenu_ItemSelected(object sender, EventArgs e)
        {
            BeginWait();
            try
            {
                IMenuItem menuItem = ((MenuItemListBoxItem)lbMainMenu.SelectedItem).Item;
                if (menuItem == lastMainMenuItem)
                    menuItem.ExecuteAgain();
                else
                {
                    lastMainMenuItem = menuItem;
                    menuItem.Execute();
                }
            }
            finally
            {
                EndWait();
            }
        }

        private void lbBrowser_ItemSelected(object sender, EventArgs e)
        {
            BeginWait();
            try
            {
                IBrowserItem browserItem = browserItems[lbBrowser.SelectedIndices[0]];
                browserItem.Execute();
            }
            finally
            {
                EndWait();
            }

        }

        private void lbSubMenu_ItemSelected(object sender, EventArgs e)
        {
            BeginWait();
            try
            {
                IMenuItem menuItem = ((MenuItemListBoxItem)lbSubmenu.SelectedItem).Item;
                if (menuItem == lastSubmenuItem)
                    menuItem.ExecuteAgain();
                else
                {
                    lastSubmenuItem = menuItem;
                    menuItem.Execute();
                }
            }
            finally
            {
                EndWait();
            }
        }

        #endregion

        #region slider

        private const int SLIDE_MARGIN = 20;

        private void SlideTo(int pos)
        {
            int newPos = - pos;
            //int step = Math.Max(1, Math.Abs((pnlSlider.Left - newPos) / 40));
            //if (pnlSlider.Left > newPos)
            //{
            //    while (pnlSlider.Left > newPos + step)
            //        pnlSlider.Left -= step;
            //}
            //else
            //{
            //    while (pnlSlider.Left < newPos - step)
            //        pnlSlider.Left += step;
            //}
            pnlSlider.Left = newPos;
        }

        public SlidePosition SlidePosition
        {
            get { return slidePosition; }
            set
            {
                if (slidePosition != value)
                {
                    slidePosition = value;
                    switch (slidePosition)
                    {
                        case SlidePosition.MainMenu:
                            SlideTo(0);
                            break;
                        case SlidePosition.Submenu:
                            SlideTo(pnlSubmenu.Left - SLIDE_MARGIN);
                            break;
                        case SlidePosition.Browser:
                            SlideTo(pnlBrowserAndInfo.Left - SLIDE_MARGIN);
                            break;
                        case SlidePosition.Playlist:
                            SlideTo(pnlPlaylistAndPreview.Left - SLIDE_MARGIN);
                            break;
                        case SlidePosition.CustomScreen:
                            SlideTo(pnlCustomScreens.Left - SLIDE_MARGIN);
                            break;
                    }

                    EventHandler temp = SlidePositionChanged;
                    if (temp != null)
                    {
                        temp(this, null);
                    }
                }
            }
        }

        public event EventHandler SlidePositionChanged;

        private void lbMainMenu_Enter(object sender, EventArgs e)
        {
            SlidePosition = SlidePosition.MainMenu;
        }

        private void lbSubMenu_Enter(object sender, EventArgs e)
        {
            SlidePosition = SlidePosition.Submenu;
        }

        private void lbBrowser_Enter(object sender, EventArgs e)
        {
            SlidePosition = SlidePosition.Browser;
        }

        private void lbPlaylist_Enter(object sender, EventArgs e)
        {
            SlidePosition = SlidePosition.Playlist;
        }

        public Control GetActivePreviewWindow()
        {
            Control parent = null;
            if (SlidePosition == SlidePosition.Playlist)
                parent = pnlPreview;
            else
            if (Program.LeisureCenterInstance.MainForm.pnlThumbnailPreview.Visible)
            {
                parent = pnlThumbnailPreview;
            }
            return parent;
        }

        public void FocusActiveList()
        {
            if (tbRandom.Focused || tbRepeat.Focused)
                return;

            switch (SlidePosition)
            {
                case SlidePosition.MainMenu:
                    lbMainMenu.Focus();
                    break;
                case SlidePosition.Submenu:
                    lbSubmenu.Focus();
                    break;
                case SlidePosition.Browser:
                    lbBrowser.Focus();
                    break;
                case SlidePosition.Playlist:
                    lbPlaylist.Focus();
                    break;
                case SlidePosition.CustomScreen:
                    pnlCustomScreens.Focus();
                    if (pnlCustomScreens.Controls.Count > 0)
                        pnlCustomScreens.Controls[0].Focus();
                    break;
            }
        }

        #endregion

        #region commands

        private void tbRepeat_Click(object sender, EventArgs e)
        {
            Program.LeisureCenterInstance.CommandsManager.EnqueueCommand(LeisureCenterCommand.ToggleRepeatMode);
        }

        private void tbRandom_Click(object sender, EventArgs e)
        {
            Program.LeisureCenterInstance.CommandsManager.EnqueueCommand(LeisureCenterCommand.ToggleRandomMode);
        }

        private void miNext_Click(object sender, EventArgs e)
        {
            Program.LeisureCenterInstance.CommandsManager.EnqueueCommand(LeisureCenterCommand.Next);
        }

        private void miStop_Click(object sender, EventArgs e)
        {
            Program.LeisureCenterInstance.CommandsManager.EnqueueCommand(LeisureCenterCommand.Stop);
        }

        private void miExit_Click(object sender, EventArgs e)
        {
            Program.LeisureCenterInstance.CommandsManager.EnqueueCommand(LeisureCenterCommand.Exit);
        }

        private void miShowHide_Click(object sender, EventArgs e)
        {
            Program.LeisureCenterInstance.CommandsManager.EnqueueCommand(LeisureCenterCommand.ShowHide);
        }

        private void pnlPreview_DoubleClick(object sender, EventArgs e)
        {
            Program.LeisureCenterInstance.CommandsManager.EnqueueCommand(LeisureCenterCommand.ToggleFullScreen);
        }

        private void previousToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Program.LeisureCenterInstance.CommandsManager.EnqueueCommand(LeisureCenterCommand.Previous);
        }

        #endregion

        #region misc

        private void timerDateTime_Tick(object sender, EventArgs e)
        {
            DateTime now = DateTime.Now;
            lblDateTime.Text = String.Format("{0:HH}:{0:mm}\r\n{0:dd}/{0:MM}", now);
            int position = Program.LeisureCenterInstance.libraryManager.pathsBrowserThread.BrowsedPathsCount;
            int count = Program.LeisureCenterInstance.libraryManager.pathsBrowserThread.PathsCount;
            if (position == count)
            {
                position = Program.LeisureCenterInstance.libraryManager.filesCheckerThread.CheckedFilesCount;
                count = Program.LeisureCenterInstance.libraryManager.filesCheckerThread.FilesCount;
            }
            lblLibrary.Text = String.Format("{0}/{1}", position, count);
            lblLibrary.Visible = position != count;
        }

        public void FocusPlaylist()
        {
            if (lbPlaylist.CanFocus)
                lbPlaylist.Focus();
        }

        public void BeginWait()
        {
            lblHourglass.Visible = true;
            Refresh();
        }

        public void EndWait()
        {
            lblHourglass.Visible = false;
        }

        #endregion

        private void pbPosition_MouseUp(object sender, MouseEventArgs e)
        {
            Program.LeisureCenterInstance.PlaylistManager.Playlist.ActivePlaylistItemOrStub.Position = e.X * Program.LeisureCenterInstance.PlaylistManager.Playlist.ActivePlaylistItemOrStub.Duration / pbPosition.ClientSize.Width;
        }

        private void panel1_Click(object sender, EventArgs e)
        {
            //throw new Exception("erreur dans le thread principal");
            Program.LeisureCenterInstance.CommandsManager.EnqueueCommand(LeisureCenterCommand.Back);
        }

        private void MainForm_Shown(object sender, EventArgs e)
        {
            StartIdle();
        }

        private void MainForm_MouseMove(object sender, MouseEventArgs e)
        {
            Point newCursorPos = Cursor.Position;
            if (lastCursorPos != newCursorPos)
            {
                lastCursorPos = newCursorPos;
                StartIdle();
            }
        }

        private void MainForm_KeyDown(object sender, KeyEventArgs e)
        {
            StartIdle();
        }

        private void lbBrowser_SelectedValueChanged(object sender, EventArgs e)
        {
            if (lbBrowser.SelectedIndex < 0)
                lblInfos.Text = "";
            else
            {
                lblInfos.Text = browserItems[lbBrowser.SelectedIndex].Description;
                string location = browserItems[lbBrowser.SelectedIndex].Thumbnail;
                if (string.IsNullOrEmpty(location))
                {
                    pbThumbnail.ImageLocation = string.Empty;
                }
                else
                {
                    pbThumbnail.LoadAsync(location);
                    pbThumbnail.BringToFront();
                }
            }
        }

        private void fastReverseToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Program.LeisureCenterInstance.CommandsManager.EnqueueCommand(LeisureCenterCommand.FastRewind);
        }

        private void fastForwardToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Program.LeisureCenterInstance.CommandsManager.EnqueueCommand(LeisureCenterCommand.FastForward);
        }

        private void lbBrowser_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 'o')
                OptionsSelect();
            else
                if (e.KeyChar == 'a')
                    AlternateSelect();
        }

        internal void OptionsSelect()
        {
            if (lbBrowser.Focused && lbBrowser.SelectedIndex > -1)
                this.browserItems[lbBrowser.SelectedIndex].OptionsExecute();
            else
                if (lbSubmenu.Focused && lbSubmenu.SelectedIndex > -1)
                    ((MenuItemListBoxItem)lbSubmenu.SelectedItem).Item.OptionsExecute();
                else
                    if (lbMainMenu.Focused && lbMainMenu.SelectedIndex > -1)
                        ((MenuItemListBoxItem)lbMainMenu.SelectedItem).Item.OptionsExecute();
        }

        internal void AlternateSelect()
        {
            if (lbBrowser.Focused && lbBrowser.SelectedIndex > -1)
                this.browserItems[lbBrowser.SelectedIndex].AlternateExecute();
            else
                if (lbPlaylist.Focused && lbPlaylist.SelectedIndex > -1)
                {
                    if (lbPlaylist.SelectedIndex == lbPlaylist.PlayingIndex)
                        Program.LeisureCenterInstance.playListManager.RemoveAllExceptCurrentItem();
                    else
                    {
                        int previousSelectedIndex = lbPlaylist.SelectedIndex;
                        Program.LeisureCenterInstance.playListManager.RemoveItem(lbPlaylist.SelectedIndex);
                        lbPlaylist.SelectedIndex = Math.Min(previousSelectedIndex, lbPlaylist.Items.Count - 1);
                    }
                }
        }

        internal void MainSelect()
        {
            if (lbBrowser.Focused)
                lbBrowser.SelectItem();
            else
                if (lbSubmenu.Focused)
                    lbSubmenu.SelectItem();
                else
                    if (lbMainMenu.Focused)
                        lbMainMenu.SelectItem();
                    else
                        if (lbPlaylist.Focused)
                            lbPlaylist.SelectItem();
                        else
                            SendKeys.Send(@" ");
        }

        private void playPauseToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Program.LeisureCenterInstance.CommandsManager.EnqueueCommand(LeisureCenterCommand.PlayPause);
        }

        private void fullScreenToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Program.LeisureCenterInstance.CommandsManager.EnqueueCommand(LeisureCenterCommand.ToggleFullScreen);
        }

        private void selectToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Program.LeisureCenterInstance.CommandsManager.EnqueueCommand(LeisureCenterCommand.Select);
        }

        private void miCrash_Click(object sender, EventArgs e)
        {
            throw new Exception("Test exception!");
        }

        private void miTest_Click(object sender, EventArgs e)
        {
            Program.LeisureCenterInstance.CommandsManager.EnqueueCommand(LeisureCenterCommand.Refresh);
        }
    }

    public class MenuItemListBoxItem : ListBoxItem
    {
        public IMenuItem Item;

        public MenuItemListBoxItem(LeisureCenter.Controls.ListBox listBox, IMenuItem item)
            : base(listBox, item.Text, item.IconName)
        {
            this.Item = item;
        }
    }

    public enum SlidePosition
    {
        MainMenu,
        Submenu,
        Browser,
        Playlist,
        CustomScreen
    }
}